import React, { useEffect, useState } from 'react'
import { useParams, useLocation } from 'react-router-dom';
interface QuestionData {
  id: number;
  des: string;
  zan: string;
  say: string;
  time: string;
}
export default function Detail() {
  const { id } = useParams();
  const location = useLocation();
  const [question, setQuestion] = useState<QuestionData | null>(null);

  useEffect(() => {
    const stateQue = location.state as QuestionData | null;
    if (stateQue) {
      setQuestion(stateQue);
      localStorage.setItem('questionData', JSON.stringify(stateQue));
    } else {
      const store = localStorage.getItem('questionData');
      if (store) {
        const storedData = JSON.parse(store);
        if (storedData.id === parseInt(id || '0', 10)) {
          setQuestion(storedData);
        }
      }
    }
  }, [location, id]);

  if (!question) {
    return <div>未获取到问题数据</div>;
  }

  return (
    <div>
      <h2>{question.des}</h2>
      <p>赞同: {question.zan}，评论: {question.say}，时间: {question.time}</p>
    </div>
  );
}
